package org.parademo.dao;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.parademo.model.CommunityEntity;

import java.util.List;

/**
 * Currently this class handles Community related queries.
 */
public class CommunityDao extends GenericDao<CommunityEntity> {

    public CommunityDao(SessionFactory factory) {
        super(factory);
    }

    public CommunityEntity retrieve(java.lang.Long communityId) {

        Query query = getCurrentSession().createQuery(
                "from\n" +
                        "    CommunityEntity c\n" +
                        "where\n" +
                        "    c.id = :communityId\n"
        );
        query.setLong("communityId", communityId);

        @SuppressWarnings("unchecked")
        List<CommunityEntity> communityEntities = query.list();

        if (communityEntities.size() == 0) {
            return null;
        }
        return communityEntities.get(0);
    }

    public void create(CommunityEntity entityToCreate) {
        getCurrentSession().save(entityToCreate);
    }

    public void update(CommunityEntity entityToUpdate) {
        throw new UnsupportedOperationException("unimplemented"); // todo: implement this
    }

    public void delete(CommunityEntity entityToDelete) {
        throw new UnsupportedOperationException("unimplemented"); // todo: implement this
    }

    @SuppressWarnings("unchecked")
    public List<CommunityEntity> getCommunities() {
        Query query = getCurrentSession().createQuery(
                "from CommunityEntity"
        );

        return query.list();
    }
}
